package com.xiaoyang.gatewayserver.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
 * @auther XiaoYang
 * @DATE 2023/5/7-11:48
 * 自定义请求限制的类
 *
 */
@Configuration
public class RequestLimitConfig {

    //针对某一个接口ip来限流 /doLogin 每一个ip10s只能访问三次
    @Bean
    @Primary
    public KeyResolver ipKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getHeaders().getHost().getHostString());
    }


    //针对路径限制 /doLogin 10只能访问三次
    //api就是接口 外面一般吧gateway  叫为 api网关 新一代网关
    @Bean
    public KeyResolver apiKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }

}
